package com.fengwk.support.code;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import com.fengwk.support.constant.SupportConstant;
import com.fengwk.support.exception.BaseException;
import com.fengwk.support.util.CodeUtils;

/**
 * AES对称加密算法
 * 
 * @author fengwk
 *
 */
public class AES implements Serializable {

	private static final long serialVersionUID = 768695910292499998L;
	
	private static final String ALGORITHM_AES = "AES";

	private String key;
	
	private Cipher encryptCipher;
	
	private Cipher decryptCipher;
	
	public AES(String key) {
		this.key = key;
	}
	
	/**
	 * 加密
	 * 
	 * @param data
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 */
	public byte[] encrypt(byte[] data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
		return encryptCipher().doFinal(data);
	}
	
	/**
	 * 加密
	 * 
	 * @param data
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 */
	public byte[] encrypt(String data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
		return encrypt(data.getBytes());
	}
	
	/**
	 * 加密为16进制
	 * 
	 * @param data
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 */
	public String encrypt2hex(byte[] data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
		return CodeUtils.binary2hex(encryptCipher().doFinal(data));
	}
	
	/**
	 * 加密为16进制
	 * 
	 * @param data
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 * @throws UnsupportedEncodingException 
	 */
	public String encrypt2hex(String data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
		return encrypt2hex(data.getBytes(SupportConstant.CHARSET_UTF8));
	}

	/**
	 * 解密
	 * 
	 * @param data
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 */
	public byte[] decrypt(byte[] data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
		return decryptCipher().doFinal(data);
	}
	
	/**
	 * 解密
	 * 
	 * @param data
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 */
	public byte[] decrypt(String data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
		return decrypt(CodeUtils.hex2binary(data));
	}
	
	/**
	 * 解密为字符串形式
	 * 
	 * @param data
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 * @throws UnsupportedEncodingException
	 */
	public String decrypt2str(byte[] data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
		return new String(decryptCipher().doFinal(data), SupportConstant.CHARSET_UTF8);
	}
	
	/**
	 * 解密为字符串形式
	 * 
	 * @param data
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 * @throws UnsupportedEncodingException
	 */
	public String decrypt2str(String data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
		return decrypt2str(CodeUtils.hex2binary(data));
	}
	
	private Cipher encryptCipher() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
		if (encryptCipher == null)
			encryptCipher = init(key, Cipher.ENCRYPT_MODE);
		return encryptCipher;
	}
	
	private Cipher decryptCipher() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
		if (decryptCipher == null)
			decryptCipher = init(key, Cipher.DECRYPT_MODE);
		return decryptCipher;
	}
	
	// 初始化aes编码器
    private Cipher init(String key, int opmode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    	if (null == key || key.length() == 0) 
    		throw new BaseException("null == key || key.length() == 0");
    	KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM_AES);
		kgen.init(128, new SecureRandom(key.getBytes()));
		SecretKeySpec secretKey = (SecretKeySpec) new SecretKeySpec(kgen.generateKey().getEncoded(), ALGORITHM_AES);
		Cipher cipher = Cipher.getInstance(ALGORITHM_AES);// 创建密码器
		cipher.init(opmode, secretKey);// 初始化
		return cipher;
    }
	
}
